﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Game_Engine
{
    public static class InputManager
    {
        private static KeyboardState prevKey
        {
            get;
            set;
        }
        private static KeyboardState currentKey
        {
            get;
            set;
        }
        private static MouseState prevMouse
        {
            get;
            set;
        }
        private static MouseState currentMouse
        {
            get;
            set;
        }
        private static Vector2 MousePosition
        {
            get;
            set;
        }

        public static void Initialize()
        {
            prevKey = currentKey = Keyboard.GetState();
            prevMouse = currentMouse = Mouse.GetState();
        }

        public static void Update()
        {
            prevKey = currentKey;
            currentKey = Keyboard.GetState();
            prevMouse = currentMouse;
            currentMouse = Mouse.GetState();
            MousePosition = new Vector2(currentMouse.X, currentMouse.Y); ;
        }

        public static bool KeyDown(Keys key)
        {
            return currentKey.IsKeyDown(key);
        }

        public static bool KeyPressed(Keys key)
        {
            return currentKey.IsKeyDown(key) &&
                prevKey.IsKeyUp(key);
        }

        public static bool KeyReleased(Keys key)
        {
            return currentKey.IsKeyUp(key) &&
                prevKey.IsKeyDown(key);
        }

        public static Vector2 GetMousePosition()
        {
            return new Vector2(MousePosition.X, MousePosition.Y);
        }

        public static bool MousePressed(int mouseButton)
        {
            switch (mouseButton)
            {
                case 0:
                    return prevMouse.LeftButton == ButtonState.Released
                        && currentMouse.LeftButton == ButtonState.Pressed;

                case 1:
                    return prevMouse.MiddleButton == ButtonState.Released
                        && currentMouse.MiddleButton == ButtonState.Pressed;

                case 2:
                    return prevMouse.RightButton == ButtonState.Released
                        && currentMouse.RightButton == ButtonState.Pressed;

                default:
                    return false;
            }
        }

        public static bool MouseReleased(int mouseButton)
        {
            switch (mouseButton)
            {
                case 0:
                    return prevMouse.LeftButton == ButtonState.Pressed
                        && currentMouse.LeftButton == ButtonState.Released;

                case 1:
                    return prevMouse.MiddleButton == ButtonState.Pressed
                        && currentMouse.MiddleButton == ButtonState.Released;

                case 2:
                    return prevMouse.RightButton == ButtonState.Pressed
                        && currentMouse.RightButton == ButtonState.Released;
                default:
                    return false;
            }
        }
    }
}
